//
//  QRegisterVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/4.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QRegisterVC.h"
#import "QWebViewVC.h"
#import "JPUSHService.h"
@interface QRegisterVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *registerBgView;//背景视图

@property (weak, nonatomic) IBOutlet UIImageView *oneLine;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *oneLineConstr;

@property (weak, nonatomic) IBOutlet UIImageView *twoLine;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *twoLineConstr;

@property (weak, nonatomic) IBOutlet UIImageView *threeLine;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *threeConstr;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;//手机号

@property (weak, nonatomic) IBOutlet UITextField *authCodeTextField;//验证码

@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;//密码

@property (weak, nonatomic) IBOutlet UIButton *getAuthCodeBtn;//获取验证码按钮

@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstrain;

@property (weak, nonatomic) IBOutlet UITextField *yPleaseCodeTextField;





@end

@implementation QRegisterVC{
    //NSString *machineCode;
    BOOL oneBool,twoBool,threeBool,fourBool;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    fourBool = YES;
    [self setupAttbuild];
}

- (void)setupAttbuild{
    
    [self setupForDismissKeyboard];//释放键盘
    [_registerButton blueStyle];
    
    _registerBgView.backgroundColor = [UIColor whiteColor];
    _oneLine.backgroundColor = [QColorUtility defaultLineColor];
    _oneLineConstr.constant = 0.5;
    _twoLine.backgroundColor = [QColorUtility defaultLineColor];
    _twoLineConstr.constant = 0.5;
    _threeLine.backgroundColor = [QColorUtility defaultLineColor];
    _threeConstr.constant = 0.5;
    _loginButton.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
    _loginButton.layer.borderWidth = 0.5;
    
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(registerBack)];

    _getAuthCodeBtn.layer.borderColor = [QColorUtility defaultBlueColor].CGColor;
    _getAuthCodeBtn.layer.borderWidth = 0.5;
    _getAuthCodeBtn.layer.cornerRadius = 4.0;
    
    _phoneTextField.tag = 1;
    _phoneTextField.delegate = self;
    [_phoneTextField addTarget:self action:@selector(textfieldAction:) forControlEvents:UIControlEventEditingChanged];
    
    _authCodeTextField.tag = 2;
    _authCodeTextField.delegate = self;
    [_authCodeTextField addTarget:self action:@selector(textfieldAction:) forControlEvents:UIControlEventEditingChanged];
    
    _passWordTextField.tag = 3;
    _passWordTextField.delegate = self;
    _passWordTextField.secureTextEntry = YES;
    [_passWordTextField addTarget:self action:@selector(textfieldAction:) forControlEvents:UIControlEventEditingChanged];
    
    _yPleaseCodeTextField.tag = 4;
    _yPleaseCodeTextField.text = _scanCodeStr;
    [_yPleaseCodeTextField addTarget:self action:@selector(textfieldAction:) forControlEvents:UIControlEventEditingChanged];

    
    
    if(iphone4x_3_5){
        self.topConstrain.constant = 80;
    }else if(iphone5x_4_0){
        self.topConstrain.constant = 90;
    }else if (iphone6_4_7){
        self.topConstrain.constant = 120;
    }else if (iphone6Plus_5_5){
         self.topConstrain.constant = 150;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textfieldAction:(UITextField *)textField{
    
    if(textField.tag == 1){
        CLog(@"电话==%@",textField.text);
        if(textField.text.length == 11){
            oneBool = YES;
        }else{
            oneBool = NO;
        }
    
    }else if (textField.tag == 2){
        if(textField.text.length == 6){
            twoBool = YES;
        }else{
            twoBool = NO;
        }
        CLog(@"验证码===%@",textField.text);
    
    }else if (textField.tag == 3){
        if(textField.text.length > 0){
            threeBool = YES;
        }else{
            threeBool = NO;
        }
        CLog(@"密码===%@",textField.text);
    }else if (textField.tag == 4){
    
         CLog(@"邀请码===%@",textField.text);
    }
}

#pragma mark---返回
- (void)registerBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark---获取验证码
- (IBAction)getAuthCodeAction:(id)sender {
    CLog(@"获取验证码");
    if(_phoneTextField.text.length != 11){
        if(_phoneTextField.text.length == 0){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }else{
        [QParamService onTimer:(UIButton *)sender type:YES];
        [self requestAuthCodel];//请求验证码
    }
}

#pragma mark---登录
- (IBAction)loginAction:(id)sender {
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark---立即注册
- (IBAction)registerAction:(id)sender {
    if(oneBool && twoBool && threeBool && fourBool){
        [self requestData];
    }else{
        if(!oneBool){
            if(_phoneTextField.text.length == 0){
               [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }else{
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
        }else if (!twoBool){
            if(_authCodeTextField.text.length == 0){
                 [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写验证码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }else{
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确验证码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
        }else if (!threeBool){
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写密码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }else if (!fourBool){
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请勾选<<用户使用协议>>" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }
}

- (IBAction)agreeBtnAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if(btn.selected){
        CLog(@"取消");
        fourBool = NO;
    }else{
        CLog(@"勾选");
        fourBool = YES;
    }
    btn.selected = !btn.selected;
}

- (IBAction)readOilAction:(id)sender {
    CLog(@"读协议");
    QWebViewVC *vc = [[QWebViewVC alloc] init];
    vc.style = WebLocalityStyle;
    vc.title = @"用户使用协议";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark-----请求验证码接口
- (void)requestAuthCodel{
    NSString *url = [QParamService urlWithAction:@"getVerificationCode"];
    NSDictionary *param = [QParamService getauthcodetel:_phoneTextField.text type:@"1"];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"验证码已发送,请注意查收" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }else{
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

#pragma mark-----请求注册接口
- (void)requestData{
    NSTimeInterval inter = [[NSDate date] timeIntervalSince1970] * 1000;//当前时间戳
    NSString *timec = [NSString stringWithFormat:@"ios_%.f",inter];
    NSString *machineCode = [NSString stringWithFormat:@"%@%@",timec,_phoneTextField.text];
    [JPUSHService setAlias:machineCode callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
    NSString *url = [QParamService urlWithAction:@"getRegister"];
    NSString *md5Str = [QMD5Hash md5String:self.passWordTextField.text];
    md5Str = [md5Str uppercaseString];
    NSDictionary *param = [QParamService registertel:self.phoneTextField.text verificationCode:self.authCodeTextField.text password:md5Str machineCode:machineCode pleaseCode:_yPleaseCodeTextField.text];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if (error) {
            return;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"注册成功!" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            QLoginModel *loginModel = [QLoginModel objectFromDict:basModel.items];
            loginModel.tel = _phoneTextField.text;
            [QDataUtility updateLoginModel:loginModel];//更新model
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRegisterSuccess object:nil];
            [self performSelector:@selector(bacPop) withObject:nil afterDelay:1];
        }else{
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)bacPop{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias {
    NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
    if(iResCode == 0){
        CLog(@"绑定别名成功");
    }else{
        CLog(@"绑定别名失败");
    }
}


@end
